//
//  TwERPVehicle.m
//  TwERP
//
//  Created by Xiangyang Li on 12-7-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "TwERPVehicle.h"
#import "TwERPJsonAgent.h"
#import "TwERPGongdan.h"
    
@implementation TwERPVehicle

@synthesize licenseNo = _licenseNo;
@synthesize driver = _driver;

@synthesize customer = _customer;
+ (TwERPVehicle *)  getVehicleByCphm:(NSString *)cphm
{
    TwERPVehicle * veh=[[TwERPVehicle alloc] init];
    NSDictionary * jsonVeh = [TwERPJsonAgent getVehicleByCphm:cphm];
    if (jsonVeh != nil) {

        [veh setValuesForKeysWithDictionary:jsonVeh];
        return veh;
    }
    else
    return nil;
}
- (TwERPCustomer *)customer
{
    if (_customer == nil) _customer = [[TwERPCustomer alloc] init];
    return _customer;
}

-(void) setCustomer:(id)customer
{
    
    if ([customer isKindOfClass:[TwERPCustomer class]])
        _customer = customer;
    else if  ([customer isKindOfClass:[NSDictionary class]])
    {
        _customer = [[TwERPCustomer alloc] init];
        [_customer setValuesForKeysWithDictionary:customer];
    }

}

- (NSArray *) getGongdanList
{
    NSArray *gdDictList=[TwERPJsonAgent getGongdanListByCphm:self.licenseNo];
    NSMutableArray *gdList = [[NSMutableArray alloc] init];
    
    for (NSDictionary *gdd in gdDictList)
    {
        TwERPGongdan * gd =[[TwERPGongdan alloc] init];
        [gd setValuesForKeysWithDictionary:gdd];
         // NSLog(@"gdd is %@",gdd);
     //   NSLog(@"gongdan is %@",[gd transFormToDictionary]);
        [gdList addObject:gd];
    }
    return gdList;
}
-(NSDictionary *)transFormToDictionary
{
    [self validateEntityId];
    NSMutableDictionary * jDic=[[NSMutableDictionary alloc] initWithDictionary:[self dictionaryWithValuesForKeys:[self allKeys]]];
    [jDic setValue:[self.customer transFormToDictionary] forKey:@"customer"];
 
    return jDic;
}

@end
